﻿// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.

using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Z.EntityFramework.Plus;

namespace Z.Test.EntityFramework.Plus
{
    public partial class QueryIncludeFilter_Where
    {
        [TestMethod]
        public void Many_Property()
        {
            Action action = () =>
            {
                TestContext.DeleteAll(x => x.Association_Multi_OneToMany_Right1s);
                TestContext.DeleteAll(x => x.Association_Multi_OneToMany_Right2s);
                TestContext.DeleteAll(x => x.Association_Multi_OneToMany_Lefts);

                using (var ctx = new TestContext())
                {
                    var left = TestContext.Insert(ctx, x => x.Association_Multi_OneToMany_Lefts, 1).First();
                    left.Right1s = TestContext.Insert(ctx, x => x.Association_Multi_OneToMany_Right1s, 5);
                    left.Right2s = TestContext.Insert(ctx, x => x.Association_Multi_OneToMany_Right2s, 5);
                    ctx.SaveChanges();
                }

                using (var ctx = new TestContext())
                {
                    var list = ctx.Association_Multi_OneToMany_Right1s
                        .Where(x => x.ColumnInt > 2)
                        .IncludeFilter(right => right.Left)
                        .IncludeFilter(right => right.Left.Right2s.Where(y => y.ColumnInt > 2))
                        .ToList()
                        .Select(x => x.Left)
                        .Distinct()
                        .ToList();

                    // TEST: context
                    Assert.AreEqual(5, ctx.ChangeTracker.Entries().Count());

                    // TEST: left
                    Assert.AreEqual(1, list.Count);
                    var item = list[0];

                    // TEST: right1
                    Assert.AreEqual(2, item.Right1s.Count);
                    Assert.AreEqual(3, item.Right1s[0].ColumnInt);
                    Assert.AreEqual(4, item.Right1s[1].ColumnInt);

                    // TEST: right2
                    Assert.AreEqual(2, item.Right2s.Count);
                    Assert.AreEqual(3, item.Right2s[0].ColumnInt);
                    Assert.AreEqual(4, item.Right2s[1].ColumnInt);
                }
            };

            MyIni.RunWithFailLogical(MyIni.GetSetupCasTest(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name), action);
        }
    }
}